package com.wondersgroup.fjzlpt.org.dao.impl.yljgry;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.org.dao.inter.yljgry.YljgryDAO;

@Repository
public class YljgryDAOImpl extends BaseJTDAOImpl implements YljgryDAO{
 
	@Override
	@Autowired
	@Qualifier("TB_ZD_YLJGRYXX")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}

	@Override
	public JSONObject queryPage(Map<String, String> map) {
		StringBuilder sbud = new StringBuilder("select a.*,b.KSMC,c.YLJGMC, \n");
		sbud.append(" (select XMMC from TB_ZY_XB where XMDM = a.XB) XBMC, \n");
		sbud.append(" (select XMMC from TB_ZY_ZC where XMDM = a.XB) ZCMC \n");
		sbud.append(" from TB_ZD_YLJGRYXX a \n");
		sbud.append("left join TB_ZD_KSXX b on a.KSDM = b.KSDM and a.PTJGID = b.PTJGID \n");
		sbud.append("left join TB_ZD_YLJGXX c on  a.PTJGID = c.PTJGID where 1=1 \n");
		/*StringBuilder sbud = new StringBuilder("select a.*,b.KSMC,c.YLJGMC,d.TYPE,d.PASSWORD,d.USERNAME,e.ROLEID from TB_ZD_YLJGRYXX a \n");
		sbud.append("left join TB_ZD_KSXX b on a.KSDM = b.KSDM and a.PTJGID = b.PTJGID \n");
		sbud.append("left join TB_ZD_YLJGXX c on  a.PTJGID = c.PTJGID \n");
		sbud.append("left join TB_AUTH_USER d on  a.USERID = d.USERID \n");
		sbud.append("left join TB_AUTH_USER_ROLE e on  a.USERID = e.USERID where 1=1  \n");*/
		List list = new ArrayList();
		String value = map.get("XM");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and a.XM like ? ");
			list.add("%"+value+"%");
		}
		value = map.get("YLJGMC");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and c.YLJGMC like ? ");
			list.add("%"+value+"%");
		}
		PageQuerySQLBean sb = checkAndCreatePageQuerySQLBean(map, sbud.toString(), "order by a.YLJGDM, a.KSDM, a.YSGH", list.toArray());
		return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
	}
	
	/**
	 * 判断记录是否存在
	 * @param sql
	 * @param args
	 * @return
	 */
	public boolean isExist(Map<String, String> map) {
		StringBuilder sbud = new StringBuilder("select count(1) from TB_ZD_YLJGRYXX where YLJGDM= ? and KSDM = ? and YSGH = ?");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[]{map.get("YLJGDM"),map.get("KSDM"),map.get("YSGH")});
		return this.isExist(sb);
	}

	@Override
	public JSONObject queryOne(Map<String, String> map) {
		StringBuilder sb = new StringBuilder("select a.USERID,a.XB,a.XM,a.SJHM,a.JG,a.CSRQ,a.CSRQ,a.ZZDH,a.SFZH,a.ZGXLDM,a.SXZYMC,a.ZGXWDM,a.CJGZRQ,a.ZC,a.YSGH,a.ZW,a.ZC, \n");
		sb.append("b.GWMCDM,b.GWLBDM,b.GWBM,b.RYSFDM,b.SFBZ, \n");
		sb.append("c.PRACTICE_JBDM,c.PRACTICE_LBDM,c.PRACTICE_FWDM,c.PRACTICE_DDZY,c.PRACTICE_BM, \n");
		sb.append("(select YLJGMC from TB_ZD_YLJGXX where PTJGID=a.PTJGID) YLJGMC, \n");
		sb.append("(select KSMC from TB_ZD_KSXX where PTJGID=a.PTJGID and KSDM=a.KSDM) KSMC ");
		sb.append("from TB_ZD_YLJGRYXX a left join TB_ZD_YLJGRY_ZYBJ b on a.USERID=b.USERID \n");
		sb.append("left join TB_ZD_YLJGRY_PRACTICE c on a.USERID=c.USERID where a.USERID = ?");
		SQLBean bean = new SQLBean(sb.toString(),map.get("USERID"));
		
		return super.queryOne(bean);
	}
	
	/**
	 * 查询多点执业信息
	 * @param map
	 * @return
	 */
	public JSONArray queryDdzy(Map<String, String> map){
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append("select b.YLJGMC,(select XMMC from TB_ZY_JGLB where XMDM=b.WSJGLBDM) WSJGLBMC \n");
		sqlBuf.append("from TB_ZD_YLJGRY_PRACTICE_MULTI a left join TB_ZD_YLJGXX b on a.PRACTICE_JGID=b.PTJGID where a.USERID = ?");
		
		SQLBean bean = new SQLBean(sqlBuf.toString(), map.get("USERID"));
		
		return super.queryList(bean);
	}
	
	/**
	 * 查询所有医疗机构人员
	 * @param map
	 * @return
	 */
	public JSONArray queryAll(Map<String, String> map){
		StringBuffer sqlBuf = new StringBuffer("select * from ").append(this.tb.getTableName());
		
		SQLBean bean = new SQLBean(sqlBuf.toString());
		
		return super.queryList(bean);
	}

	/**
	 * 查询医疗机构人员信息
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryUserXx(Map<String, String> map){
		List sqlParams = new ArrayList();
		StringBuffer sqlBuf = new StringBuffer("select USERID from ").append(this.tb.getTableName())
				.append(" where 1=1 ");

		//身份证号
		String SFZH = map.get("SFZH");
		if(!StringUtils.isEmpty(SFZH)){
			sqlBuf.append("and SFZH = ?");
			sqlParams.add(SFZH);
		}

		SQLBean bean = new SQLBean(sqlBuf.toString(), sqlParams.toArray());
		return super.queryOne(bean);
	}
}
